package com.shujia.wyh.mrdemos;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;



public class WordCountDemo {
    public static void main(String[] args) throws Exception {
        //获取集群中的hadoop的环境配置
        Configuration conf = new Configuration();

        //创建一个作业，来进行单词统计
        Job job = Job.getInstance(conf);
        job.setJobName("统计哈利波特的一段英文单词个数");

        //设置作业运行的主类
        job.setJarByClass(WordCountDemo.class);

        //设置reduce的个数，直接影响到分区的编号
        job.setNumReduceTasks(2);

        //设置map类
        job.setMapperClass(MyMapper.class);
        //在hadoop环境中，数据类型与java的类型不太一样
        //java中字符串是String类型，Hadoop中字符串是Text类型
        //java中长整型是long类型，Hadoop中长整型是LongWriteable
        //设置map的输出key的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //设置reduce的类
        job.setReducerClass(MyReducer.class);
        //设置reduce的输出key-value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //设置HDFS文件的输入路径和输出路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //启动MapReduce作业
        job.waitForCompletion(true);


    }
}
